package com.xk857.article.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xk857.article.mapper.provider.LabelProvider;
import com.xk857.article.req.LabelREQ;
import com.xk857.entities.Label;
import org.apache.ibatis.annotations.*;

import java.util.List;


/**
 * 星空小屋-知识点表 Mapper 接口
 *
 * @author CV大魔王
 * @since 2021-02-21
 */
public interface LabelMapper extends BaseMapper<Label> {
    /**
     * 如果自定义sql要分页查询，只要在mapper写不带分页功能的查询sql语句，mybatis-plus它会自动将sql进行分页
     * 第1个参数必须传入分页对象 Page, 第2个参数是你查询条件的参数, 取一个别名
     * 最终会将查询到的数据封装到 IPage实现类中
     * @return 分页后的结果
     */
    @SelectProvider(type = LabelProvider.class,method = "queryPage")
    @Results({
            @Result(column = "category_id",property = "categoryId"),
            @Result(property = "categoryName", column = "category_id", javaType = String.class, one = @One(select = "com.xk857.article.mapper.CategoryMapper.findCategoryNameByLabelId"))
    })
    IPage<Label> queryPage(IPage<Label> page, @Param("req") LabelREQ req);


    @Select("SELECT xl.id,xl.name,xl.remark,xl.title_img,count( xal.id) as acount FROM  (xk857_label xl LEFT JOIN  xk857_article_label xal on xl.id = xal.label_id) LEFT JOIN xk857_article xas on xas.id = xal.article_id WHERE category_id =  #{categoryId}  GROUP BY xl.id  ORDER BY sort AND xl.update_date DESC")
    List<Label> findListByCategoryId(String categoryId);

    /**
     * 根据文章id查询知识点id集合
     * @param articleId 文章id
     * @return 知识点id集合
     */
    @Select("SELECT label_id FROM xk857_article_label WHERE article_id = #{articleId}")
    List<String> findLabelIdByArticleId(String articleId);


    /**
     * 根据文章id查询知识点对象集合
     * @param articleId 文章id
     * @return 知识点对象集合
     */
    @Select("SELECT name FROM xk857_label WHERE id in (SELECT label_id FROM xk857_article_label WHERE article_id = #{articleId})")
    List<String> findLabelListByArticleId(String articleId);

}
